//
//  SNKPrettyGirlController.m
//  SleepNews
//
//  Created by Kavee DJ on 2017/2/20.
//  Copyright © 2017年 Kavee DJ. All rights reserved.
//

#import "SNKPrettyGirlController.h"

#import "SNKPrettyGirlData.h"
#import "SNKPrettyGirlCell.h"

#import <MJRefresh.h>
#import <MJExtension.h>
#import <PPNetworkHelper.h>
#import <DKNightVersion.h>

@interface SNKPrettyGirlController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray<SNKPrettyGirlData *> *girlsArray;

@end

@implementation SNKPrettyGirlController

/* cell的重用标识 */
static NSString * const SNKPrettyGirlCellId = @"PrettyGirlCell";
static NSString *girlBaseUrlGet = @"https://c.m.163.com/recommend/getChanListNews?channel=T1456112189138&size=10";

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.myTableView];
    
    //第一次直接刷新
    [self.myTableView.mj_header beginRefreshing];
}

#pragma mark - TableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.girlsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SNKPrettyGirlCell *cell = [tableView dequeueReusableCellWithIdentifier:SNKPrettyGirlCellId forIndexPath:indexPath];
    cell.prettyGirlData = self.girlsArray[indexPath.row];
    return cell;
}

#pragma mark - TableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.girlsArray[indexPath.row].cellHeight;
}

#pragma mark - Getter and Setter
- (UITableView *)myTableView {
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.dk_backgroundColorPicker = DKColorPickerWithKey(SEP);
        
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNKPrettyGirlCell class]) bundle:nil] forCellReuseIdentifier:SNKPrettyGirlCellId];
        
        // 设置下拉刷新控件
        IMP_BLOCK_SELF(SNKPrettyGirlController);
        _myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [block_self requestNewData];
        }];
        
        // 设置上拉加载更多
        _myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [block_self requestMoreData];
        }];
    }
    return _myTableView;
}

#pragma mark - Private Method
// 上拉,加载更多旧数据
- (void)requestMoreData {
    self.currentPage ++;
    [self getTableViewData];
}

// 下拉,刷新数据
- (void)requestNewData {
    // 清零
    self.currentPage = 0;
    [_girlsArray removeAllObjects];
    [self getTableViewData];
}

// 纯粹的加载列表
- (void)getTableViewData {
    IMP_BLOCK_SELF(SNKPrettyGirlController);
    
    [PPNetworkHelper GET:girlBaseUrlGet parameters:nil responseCache:^(id responseCache) {
        if (![PPNetworkHelper isNetwork]) {
            NSArray *girlsObjArray = [SNKPrettyGirlData mj_objectArrayWithKeyValuesArray:responseCache[@"美女"]];
            [block_self.girlsArray addObjectsFromArray:girlsObjArray];
            [block_self.myTableView reloadData];
            [block_self.myTableView.mj_header endRefreshing];
            [block_self.myTableView.mj_footer endRefreshing];
        }
    } success:^(id responseObject) {
        NSArray *girlsObjArray = [SNKPrettyGirlData mj_objectArrayWithKeyValuesArray:responseObject[@"美女"]];
        [block_self.girlsArray addObjectsFromArray:girlsObjArray];
        [block_self.myTableView reloadData];
        [block_self.myTableView.mj_header endRefreshing];
        [block_self.myTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [block_self.myTableView.mj_header endRefreshing];
        [block_self.myTableView.mj_footer endRefreshing];
    }];
}

#pragma mark - Getter and Setter
- (NSMutableArray *)girlsArray {
    if (_girlsArray == nil) {
        _girlsArray = [NSMutableArray array];
    }
    return _girlsArray;
}


@end
